package com.example.taobao

import android.annotation.SuppressLint
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.os.Handler
import android.os.Message
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import kotlinx.android.synthetic.main.activity_mylove.*
import kotlinx.android.synthetic.main.activity_myorder.*
import kotlinx.android.synthetic.main.myaddress.*
import okhttp3.OkHttpClient
import okhttp3.Request
import org.json.JSONArray
import kotlin.concurrent.thread

class Mylove : AppCompatActivity() {
    private var lovelist:ArrayList<ginfo> = ArrayList<ginfo>()
    val handle = @SuppressLint("HandlerLeak")
    object : Handler()
    {
        override fun handleMessage(msg: Message) {
            lovelist.clear()
            lovelist = msg.obj as ArrayList<ginfo>
            val layoutManager = LinearLayoutManager(this@Mylove)
            loverecyclerview.layoutManager = layoutManager
            val adapter = loverAdapter(lovelist)
            loverecyclerview.adapter = adapter
            if(!lovelist.isEmpty()) {
                loverecyclerview.smoothScrollToPosition(lovelist.size - 1)
            }
        }
    }
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_mylove)
        initData()
    }
    private class loveViewHolder(view: View) : RecyclerView.ViewHolder(view)
    {
        val tvordername: TextView =view.findViewById(R.id.tv_merchandise_name1)
        val tvdescribe : TextView = view.findViewById(R.id.tv_merchandise_content1)
        val img :ImageView = view.findViewById(R.id.img_merchandise2)
    }

    private inner  class loverAdapter(val orderlist: List<ginfo>) :
        RecyclerView.Adapter<loveViewHolder>() {
        private var itemClickListener: IKotlinItemClickListener? = null
        override fun getItemCount() = lovelist.size
        override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): loveViewHolder {
            val view = LayoutInflater.from(parent.context).inflate(R.layout.lovedetail, parent, false)
            return loveViewHolder(view)
        }

        override fun onBindViewHolder(holder: loveViewHolder, position: Int) {
            val info = lovelist[position]
            holder.tvordername.text = info.name
            holder.tvdescribe.text = info.detail
            Glide.with(this@Mylove)
                .load(info.pic)
                .into(holder.img)
            holder.itemView.setOnClickListener {
                itemClickListener
            }

        }
        fun setOnKotlinItemClickListener(itemClickListener: IKotlinItemClickListener) {
            this.itemClickListener = itemClickListener
        }

    }
    fun initData() {
        lovelist.clear()
        thread {
            try {
                var addr :ArrayList<ginfo> = ArrayList<ginfo>()
                val client = OkHttpClient()
                val request = Request.Builder()
                    .url("http://192.168.43.140:8080/aaa_war_exploded/getalllove.jsp")
                    .build()
                val respone = client.newCall(request).execute()
                val responeData = respone.body!!.string()
                addr = parseJosn(responeData)
                val msg = Message()
                msg.obj = addr
                handle.sendMessage(msg)
            }
            catch (e:Exception)
            {
                e.printStackTrace()
            }
        }
    }
    private fun parseJosn(jsonStr:String):ArrayList<ginfo>
    {
        val address :ArrayList<ginfo> =  ArrayList<ginfo>()
        try {
            val jaonArray = JSONArray(jsonStr)
            for (i in 0 until jaonArray.length())
            {
                val jsonObject = jaonArray.getJSONObject(i)
                val name = jsonObject.getString("name")
                val describe = jsonObject.getString("describe")
                val pic = jsonObject.getString("pic")
                address.add(ginfo("${pic}","${name}","${describe}"))
            }
        }
        catch (e: Exception)
        {
            e.printStackTrace()
        }
        return address
    }
}